/*

This software is licensed under the terms of the MIT licence.

Copyright (c) 2009 Mihaly Heder

Permission is hereby granted, free of charge, to any person obtaining a copy of 
this software and associated documentation files (the "Software"), to deal in the 
Software without restriction, including without limitation the rights to use, copy, 
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
and to permit persons to whom the Software is furnished to do so, subject to the 
following conditions:

The above copyright notice and this permission notice shall be included in all copies 
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.

*/

package jsentence;

import info.mihaly.heder.xmlutils.XMLHelper;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class JSentenceBlocks {


	/**
	 * @param args
	 */
	public static void main(String[] args) {

		File inFile = null;

		if (args.length !=  2) {
			System.out
					.println("Usage: jsentence <JSentence configfile> <input file>");
			System.exit(1);
		}

		String configFile = args[0];
		
		inFile = new File(args[1]);

		
		try {
			Document blocksDoc = XMLHelper.getDocumentFromXmlFile(inFile);

	//		System.out.println("Getting tokens...");
			NodeList blocks = XMLHelper.getNodeListByXPath(blocksDoc,"//*[local-name(.)='block']");
			System.out.println("<sentence-ends>");
			for(int bi = 1; bi <= blocks.getLength();bi++) {
				System.out.println("   <block pos='"+bi+"'>");
	//			System.out.println(XMLHelper.getStringByXPath(blocksDoc, "local-name(/*[1])"));
				//NodeList tokens = XMLHelper.getNodeListByXPathNs(blocksDoc,"bl","http://docuphet.net/blocks","//bl:*");
				NodeList tokens = XMLHelper.getNodeListByXPath(blocksDoc,"//*[local-name(.)='block']["+bi+"]//*[local-name(.) = 'show']/text()");
	
				
				int lengthIndex=0;
				StringBuffer is = new StringBuffer();
				for (int i = 0; i < tokens.getLength(); i++) {
					String token = tokens.item(i).getNodeValue();
	//				lengthIndex += token.length()+1;
					//System.out.println(token + " - " + lengthIndex);
					is.append(token);
					is.append(' ');
				}
	
				//JSentence jsencence = new JSentence(configFile,"String",inFile,"None",outFile,is.toString());
				JSentence jsencence = new JSentence(configFile,"UTF-8");
				jsencence.execute(is.toString());
				
				List<Integer> indexes = jsencence.getIndexes();
				
				int ixi = 0;
				for (int i = 0; i < tokens.getLength(); i++) {
					String token = tokens.item(i).getNodeValue();
					lengthIndex += token.length()+1;
					//System.out.println(token + " - " + lengthIndex);
					if (ixi < indexes.size() && lengthIndex >= indexes.get(ixi)) {
						System.out.println("      <token pos='"+(i+1)+"'/>");
						ixi++;
					}
				}
				System.out.println("   </block>");
			}
			System.out.println("</sentence-ends>");
		
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}

}



